//
//  ByBooksVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/11/30.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByBooksVC.h"
#import "ByTitleValueView.h"
#import "ByTitleImageView.h"

#import "ByRecordWorkListVC.h" //记工清单 工人端
#import "ByWageListVC.h"
#import "ByCheckAccountVC.h"
#import "ByNewsVC.h"
#import "ByAccountDetailVC.h"

#import "ByRecordWorkListForemanVC.h"//记工清单 工头端

@interface ByBooksVC ()

@property (nonatomic, strong) ByNetModel *monthlyModel;
@property (nonatomic, strong) ByNetModel *totalModel;

@property (nonatomic, strong) ByTitleValueView *leftView;
@property (nonatomic, strong) ByTitleValueView *rightView;

@property (nonatomic, strong) UILabel *countLabel;


@end

@implementation ByBooksVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (BY_USER.isWorker.integerValue == 1) {
        self.title = @"工人账本";
    }else{
        self.title = @"工头账本";
    }
    

    [self initViews];
    [self addRightBarButtonItem];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self loadData];
    
}

- (void)loadData{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.mobile = BY_USER.mobile;
    
    if (BY_USER.isWorker.integerValue == 1){
        
        [ByApiRecord financeBillWorker:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            
            NSDictionary *monthlyDic = result.data[@"monthly"];
            NSDictionary *totalDic = result.data[@"year"];
            
            self.monthlyModel = [[ByNetModel alloc] initContentWithDic:monthlyDic];
            self.totalModel = [[ByNetModel alloc] initContentWithDic:totalDic];
            self.leftView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.monthlyModel.income.floatValue];
            self.rightView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.totalModel.income.floatValue];
            
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        
        [ByApiRecord financeBillOverseer:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            NSDictionary *dic = result.data[@"year"];
            NSDictionary *monthlyDic = result.data[@"monthly"];
            self.totalModel = [[ByNetModel alloc] initContentWithDic:dic];
            self.monthlyModel = [[ByNetModel alloc] initContentWithDic:monthlyDic];
            self.leftView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.monthlyModel.debt.floatValue];
            self.rightView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.totalModel.debt.floatValue];
            
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }
    
    netModel.uid = BY_USER.uid;
    [ByApiNews messageUnreadCount:netModel success:^(ByNetBaseResult *result){
        NSString *count = result.data;
        if (count.integerValue == 0) {
            self.countLabel.hidden = YES;
        }else if (count.integerValue > 0){
            self.countLabel.hidden = NO;
            self.countLabel.text = count;
        }
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
   
}


- (void)addRightBarButtonItem{
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 35, 35);
    [rightBtn setImage:[UIImage imageNamed:@"xiaoxi"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *containView = [[UIView alloc] initWithFrame:rightBtn.bounds];
    [containView addSubview:rightBtn];
    
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, -3, 22, 20)];
    [containView addSubview:countLabel];
    countLabel.backgroundColor = [UIColor redColor];
    countLabel.layer.cornerRadius = 9;
    countLabel.layer.masksToBounds = YES;
    countLabel.font = FONT(12);
    countLabel.textColor = [UIColor whiteColor];
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.text = @"100";
    countLabel.hidden = YES;
    self.countLabel = countLabel;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:containView];
}

- (void)btnAction:(UIButton *)btn{
    ByNewsVC *vc = [[ByNewsVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

- (void)initViews{
    
    self.view.backgroundColor = rgba(249, 249, 249, 1);
    CGFloat width = (SCREEN_WIDTH - 3) / 2.0;
    CGFloat height = width * 0.6;
    
    ByTitleValueView *leftView = [[ByTitleValueView alloc] init];
    [self.view addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    
    ByTitleValueView *rightView = [[ByTitleValueView alloc] init];
    [self.view addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    rightView.titleLabel.text = @"今年记账收入";
    
    self.leftView = leftView;
    self.rightView = rightView;
    
    
    ByTitleImageView *leftImage = [[ByTitleImageView alloc] init];
    [self.view addSubview:leftImage];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(leftView.mas_bottom).offset(3);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    leftImage.imageView.image = [UIImage imageNamed:@"jigongqingdan"];
    
    ByTitleImageView *rightImage = [[ByTitleImageView alloc] init];
    [self.view addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(leftView.mas_bottom).offset(3);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    rightImage.titleLabel.text = @"工资清单";
    rightImage.imageView.image = [UIImage imageNamed:@"gongziqingdan"];
    
    
    
    UIButton *booksBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:booksBtn];
    [booksBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.top.equalTo(rightImage.mas_bottom).offset(30);
        make.height.mas_equalTo(@50);
    }];
    if (BY_USER.isWorker.integerValue == 1) {
        [booksBtn setTitle:@"核账" forState:UIControlStateNormal];
    }else{
        [booksBtn setTitle:@"审帐" forState:UIControlStateNormal];
    }
    
    booksBtn.backgroundColor = BY_THEME_COLOR;
    [booksBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    booksBtn.layer.cornerRadius = 25;
    booksBtn.layer.masksToBounds = YES;
    [booksBtn addTarget:self action:@selector(booksAction:) forControlEvents:UIControlEventTouchUpInside];
    
    leftView.tag = 100;
    rightView.tag = 200;
    leftImage.tag = 300;
    rightImage.tag = 400;

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [leftView addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [rightView addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [leftImage addGestureRecognizer:tap3];
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [rightImage addGestureRecognizer:tap4];
    
}

- (void)booksAction:(UIButton *)btn{
    
    ByCheckAccountVC *vc = [[ByCheckAccountVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (tap.view.tag == 100) {
       
    }else if (tap.view.tag == 200){
        
       
    }else if (tap.view.tag == 300){
        
        if (BY_USER.isWorker.integerValue == 1) {//工人
            
            ByRecordWorkListVC *vc = [[ByRecordWorkListVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
        }else{
            
            ByRecordWorkListForemanVC *vc = [[ByRecordWorkListForemanVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
        }
       
        
    }else if (tap.view.tag == 400){
        
        ByWageListVC *vc = [[ByWageListVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
